/*!
 * \file Sockets.cpp
 * \brief Implements the functions described in Socket.h
 *
 * \author Matthew Klupchak
 * \author Andres J. Tack
 */
#include "Socket.h"

using namespace Networking;

Socket::Socket()
{
	this->m_socket = INVALID_SOCKET;

	this->InitializeLibrary();

	::memset( &( this->m_socketAddress ), 0, sizeof(( this->m_socketAddress )));
}

Socket::~Socket( void )
{
	if ( this->m_socket == INVALID_SOCKET )
	{
		std::cerr << "Socket should never be null!" << std::endl;
	}

#ifdef WIN32
	closesocket( this->m_socket );
	WSACleanup();
#else
	close( this->m_socket );
#endif
}

void Socket::InitializeLibrary( void )
{
#ifdef WIN32
	WORD wVersionRequested;          /* socket dll version info */ 
	WSADATA wsaData;                 /* data for socket lib initialisation */

	wVersionRequested = MAKEWORD( 2, 2 );

	if ( WSAStartup( wVersionRequested, &wsaData ) != 0 )
	{
		throw SocketException();
	}
#endif
}
